package com.yuma.rabbitmq.work.main;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.yuma.rabbitmq.work.util.ConnectionUtil;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Productor {
    private static final String QUEUE_NAMEA = "queue0";
    private static final String QUEUE_NAMEB = "queue1";
    private static final String EXCHANGE_NAME = "test_direct";

    public static void main(String[] args) throws IOException, TimeoutException {
        Connection connection = ConnectionUtil.getConnection();
        Channel channel = connection.createChannel();
        // 定义交换机
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT, true);
        channel.queueDeclare(QUEUE_NAMEA, false, false, false, null);
        channel.queueDeclare(QUEUE_NAMEB, false, false, false, null);

        // 绑定
        channel.queueBind(QUEUE_NAMEA, EXCHANGE_NAME, "key0");
        channel.queueBind(QUEUE_NAMEB, EXCHANGE_NAME, "key1");

        // 发送的时候：交换机、路由键、消息  和队列没关系
        channel.basicPublish(EXCHANGE_NAME, "key0", null, ("hello queue0").getBytes());
        channel.basicPublish(EXCHANGE_NAME, "key1", null, ("hello queue1").getBytes());
        channel.close();
        connection.close();
    }
}
